package com.haodf.skeletonscreen

import android.app.Activity
import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import android.widget.*
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.haodf.skeleton.ItemParams
import com.haodf.skeleton.ItemRect
import com.haodf.skeleton.SkeletonLayout
import com.haodf.skeleton.anim.ILoadingAnimtor
import com.haodf.skeleton.anim.RuoYinRuoXianLoadingAnimtor

class SecondActivity : Activity() {
    private val mIvBack: ImageView by lazy(LazyThreadSafetyMode.NONE) { findViewById<ImageView>(R.id.iv_back) }
    private val mTvTitle: TextView by lazy(LazyThreadSafetyMode.NONE) { findViewById<TextView>(R.id.tv_title) }
    private val mRlTitle: RelativeLayout by lazy(LazyThreadSafetyMode.NONE) {
        findViewById<RelativeLayout>(
            R.id.rl_title
        )
    }
    private val mTitle: TextView by lazy(LazyThreadSafetyMode.NONE) { findViewById<TextView>(R.id.title) }
    private val mLine: ImageView by lazy(LazyThreadSafetyMode.NONE) { findViewById<ImageView>(R.id.line) }
    private val mContent11: LinearLayout by lazy(LazyThreadSafetyMode.NONE) {
        findViewById<LinearLayout>(
            R.id.content_1_1
        )
    }
    private val mTv2: TextView by lazy(LazyThreadSafetyMode.NONE) { findViewById<TextView>(R.id.tv2) }
    private val mContent13: LinearLayout by lazy(LazyThreadSafetyMode.NONE) {
        findViewById<LinearLayout>(
            R.id.content_1_3
        )
    }
    private val mSkeletonSl: SkeletonLayout by lazy(LazyThreadSafetyMode.NONE) {
        findViewById<SkeletonLayout>(R.id.skeleton_sl).apply {
            this.config {
                /*listviewItemType = ItemParams(
                    listOf(
                        ItemParams.LineSegmentGroup(
                            line = 0,
                            graySegments = listOf(
                                ItemParams.GraySegment(0 to 3),
                                ItemParams.GraySegment(4 to 9),
                                ItemParams.GraySegment(18 to 19),
                            )
                        ),
                        ItemParams.LineSegmentGroup(
                            line = 1,
                            graySegments = listOf(
                                ItemParams.GraySegment(0 to 3, true),
                                ItemParams.GraySegment(4 to 9),
                                ItemParams.GraySegment(19 to 19)
                            )
                        ),
                        ItemParams.LineSegmentGroup(
                            line = 2,
                            graySegments = listOf(
                                ItemParams.GraySegment(0 to 3, true),
                                ItemParams.GraySegment(4 to 17)
                            )
                        ),
                        ItemParams.LineSegmentGroup(
                            line = 4,
                            graySegments = listOf(
                                ItemParams.GraySegment(4 to 18)
                            )
                        ),
                        ItemParams.LineSegmentGroup(
                            line = 5,
                            graySegments = listOf(
                                ItemParams.GraySegment(4 to 18)
                            )
                        ),
                        ItemParams.LineSegmentGroup(
                            line = 6,
                            graySegments = listOf(
                                ItemParams.GraySegment(4 to 7),
                                ItemParams.GraySegment(9 to 11)
                            )
                        ),
                        ItemParams.LineSegmentGroup(
                            line = 7,
                            graySegments = listOf(
                                ItemParams.GraySegment(4 to 10),
                                ItemParams.GraySegment(12 to 13),
                                ItemParams.GraySegment(18 to 19)
                            )
                        )

                    )
                )
*/

                listviewItemType = ItemParams(listOf(
                    ItemParams.LineSegmentGroup(0, listOf(
                        ItemParams.GraySegment(0 to 2),
                        ItemParams.GraySegment(3 to 7),
                        ItemParams.GraySegment(8 to 12)
                    )),
                    ItemParams.LineSegmentGroup(1, listOf(
                        ItemParams.GraySegment(0 to 2, true),
                        ItemParams.GraySegment(3 to 15),
                        ItemParams.GraySegment(19 to 19, verticalOffset = -30),
                    ))
                )
                ).also {
                    it.columnNum = 20
                }
//                listviewItemType = ItemRect.ITEM_TYPE_2
                loadingAnim = ILoadingAnimtor.TYPE_BAI_JV_GUO_XI
                skeletonEnable = true
                customLoadingAnim = null
            }
        }
    }
    private val mLoadingBtn: Button by lazy(LazyThreadSafetyMode.NONE) { findViewById<Button>(R.id.loading_btn) }
    private val mHuifuBtn: Button by lazy(LazyThreadSafetyMode.NONE) { findViewById<Button>(R.id.huifu_btn) }
    private val mLineBtn: Button by lazy(LazyThreadSafetyMode.NONE) { findViewById<Button>(R.id.line_btn) }
    private val mRecycler: RecyclerView by lazy(LazyThreadSafetyMode.NONE) {
        findViewById<RecyclerView>(
            R.id.recycler
        )
    }


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        actionBar?.hide()
        mSkeletonSl.loading()
        mHuifuBtn.setOnClickListener {
//            mTip.text = "sdfasdsadsadasdsa"
//            mTv2.text = "sadhjksadhksaj"
            mSkeletonSl.normal()
        }
//
        mLoadingBtn.setOnClickListener {
            mSkeletonSl.loading()
        }

        var i = 0
        mLineBtn.setOnClickListener {
            mSkeletonSl.config {
//                listviewItemType = 4
                skeletonEnable = false
            }
        }

        mTv2.setOnClickListener {
            Toast.makeText(this, "hahahaha", Toast.LENGTH_SHORT).show()
        }

        mRecycler.layoutManager = LinearLayoutManager(this)
        mRecycler.adapter = object : RecyclerView.Adapter<Holder>() {
            override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): Holder {
                return Holder(View.inflate(this@SecondActivity, R.layout.rv_item, null))
            }

            override fun onBindViewHolder(holder: Holder, position: Int) {
                holder.tv.text = "这是列表展示的正常 item $position"
            }

            override fun getItemCount(): Int {
                return 6
            }
        }
    }

    class Holder(view: View) : RecyclerView.ViewHolder(view) {
        val tv: TextView by lazy(LazyThreadSafetyMode.NONE) { view.findViewById<TextView>(R.id.tv) }

    }
}